package com.ruoyi.oa.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.oa.domain.LeaveTeacher;
import com.ruoyi.oa.service.ILeaveTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信请假Controller
 * 
 * @author 张艳羽
 * @date 2024-10-10
 */
@RestController
@RequestMapping("/wxapi/leave")
public class WxLeaveController extends BaseController
{
    @Autowired
    private ILeaveTeacherService leaveTeacherService;

    /**
     * 查询请假列表
     */
    @GetMapping("/listLeaveTea")
    public AjaxResult listLeaveTea(Integer leaverId, String type, String status, Integer pageNum, Integer pageSize, String orderBy)
    {
        Long leaverIdLong = null != leaverId ? Long.valueOf(leaverId) : null;
        LeaveTeacher lt = new LeaveTeacher();
        lt.setLeaverId(leaverIdLong);
        if(StringUtils.isNotEmpty(status)) {
            lt.setStatus(status);
        }
        if(StringUtils.isNotEmpty(type)) {
            lt.setType(type);
        }
        if(null != pageNum && null != pageSize) {
            orderBy = SqlUtil.escapeOrderBySql(orderBy);
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(Boolean.TRUE);
        }
        List<LeaveTeacher> list = leaveTeacherService.selectLeaveTeacherList(lt);
        long total = new PageInfo(list).getTotal();
        return AjaxResult.success(list).put("total", total);
    }

    /**
     * 新增请假
     */
    @PostMapping("/addLeaveTea")
    @ResponseBody
    public AjaxResult addLeaveTea(@RequestBody JSONObject object) throws ParseException
    {
        Integer leaverIdInt = (Integer)object.get("leaverId");
        String type = (String) object.get("type");
        String durationStr = (String) object.get("duration");
        String durType = (String)object.get("durType");
        String reason = (String)object.get("reason");
        String startTimeStr = (String)object.get("startTime");
        String endTimeStr = (String)object.get("endTime");
        if(null == leaverIdInt || StringUtils.isEmpty(type) || StringUtils.isEmpty(durationStr) || StringUtils.isEmpty(durType)
            || StringUtils.isEmpty(startTimeStr) || StringUtils.isEmpty(endTimeStr)) {
            return AjaxResult.error("请检查参数！");
        } else {
            Float duration = Float.parseFloat(durationStr);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date startTime = sf.parse(startTimeStr);
            Date endTime = sf.parse(endTimeStr);
            Long leaverId = Long.valueOf(leaverIdInt);

            LeaveTeacher lt = new LeaveTeacher();
            lt.setLeaverId(leaverId);
            lt.setType(type);
            lt.setDuration(duration);
            lt.setDurType(durType);
            lt.setReason(reason);
            lt.setStartTime(startTime);
            lt.setEndTime(endTime);

            return toAjax(leaveTeacherService.insertLeaveTeacher(lt));
        }
    }
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(@RequestBody JSONObject object)
    {
        Integer id = (Integer)object.get("id");
        Long leaverId = Long.valueOf(id);
        return toAjax(leaveTeacherService.deleteLeaveTeacherById(leaverId));
    }


}
